package net.zoneland.knowledge.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.zoneland.knowledge.cache.RedisCacheUtil;
import net.zoneland.knowledge.config.BizConfig;
import net.zoneland.knowledge.dao.QaSatisfactionMapper;
import net.zoneland.knowledge.model.QaSatisfaction;
import net.zoneland.knowledge.model.QaSatisfactionExample;
import net.zoneland.knowledge.service.NotifyService;
import net.zoneland.knowledge.service.QaSatisfactionService;
import net.zoneland.knowledge.service.RoleService;
import net.zoneland.uniflow.client.UserInfo;

/**
 * .
 *
 * @author: bcc
 * 2021/6/9
 */
@Service
@Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
public class QaSatisfactionServiceImpl implements QaSatisfactionService {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(QaSatisfactionServiceImpl.class);
    /**
     * 满意度数据接口.
     */
    @Autowired
    private QaSatisfactionMapper qaSatisfactionMapper;
    /**
     * 统一通知业务接口.
     */
    @Autowired
    private NotifyService notifyService;
    /**
     * 角色接口.
     */
    @Autowired
    private RoleService roleService;
    /**
     * redis工具.
     */
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    /**
     * 业务配置.
     */
    @Autowired
    private BizConfig bizConfig;

    /**
     * 保存满意度.
     * @param qaSatisfaction 满意度
     */
    @Override
    public void save(final QaSatisfaction qaSatisfaction) {
        qaSatisfactionMapper.insert(qaSatisfaction);
    }

    /**
     * 更新满意度.
     * @param qaSatisfaction 满意度
     */
    @Override
    public void update(final QaSatisfaction qaSatisfaction) {
        try {
            UserInfo[] noSatisfyHandlers = null;
            // fixme 不满意不走流程
//            boolean needStartProcess = qaSatisfaction.getStatus() == 0
//                && StringUtils.isNotBlank(qaSatisfaction.getQueName());
//            if (needStartProcess) {
//                noSatisfyHandlers = getNoSatisfyHandlers(qaSatisfaction);
//                if (ArrayUtils.isNotEmpty(noSatisfyHandlers)) {
//                    qaSatisfaction.setHandlerName(Stream.of(noSatisfyHandlers).map(UserInfo::getUserName)
//                        .collect(Collectors.joining(Constants.STRING_SPLIT)));
//                    qaSatisfaction.setHandlerUid(Stream.of(noSatisfyHandlers).map(UserInfo::getUserId)
//                        .collect(Collectors.joining(Constants.STRING_SPLIT)));
//                }
//            }
//            // 如果用户选择为不满意，且意见为空，则设置默认的系统意见，但是不发起流程
//            if (qaSatisfaction.getStatus() == 0 && StringUtils.isBlank(qaSatisfaction.getQueName())) {
//                qaSatisfaction.setStatus(1);
//                qaSatisfaction.setQueName(BizConstant.NO_OPINION_SATISFY_CONTENT);
//            }
            //保存数据
            qaSatisfactionMapper.updateByPrimaryKey(qaSatisfaction);
//            if (needStartProcess) {
//                this.startSatisfyProcess(qaSatisfaction, noSatisfyHandlers);
//            }
        } catch (Exception e) {
            LOGGER.error("更新满意度异常", e);
            throw new RuntimeException("更新满意度异常!");
        }
    }


    /**
     * 查询sessionId对应的满意度.
     * @param talkSessionId sessionId
     * @return 满意度列表
     */
    @Override
    public List<QaSatisfaction> queryBySessionId(final String talkSessionId) {
        final QaSatisfactionExample example = new QaSatisfactionExample();
        example.createCriteria().andSessionIdEqualTo(talkSessionId);
        return qaSatisfactionMapper.selectByExample(example);
    }

}
